package editorsvg.propiedades;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GraphicsEnvironment;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.event.DocumentListener;

/**
 * Dialogo de selección de fuente.
 */
public class JFontChooser extends JDialog {
    
    private static final long serialVersionUID = 1L;
    public static int OK_OPTION = 0;
    public static int CANCEL_OPTION = 1;
    
    private JList fontList, sizeList;
    private JTextField sizeField;
    private JCheckBox cbBold, cbItalic;
    private JTextArea txtSample;
    
    private int OPTION;
    
    private String[] sizes = new String[]
    { "2","4","6","8","10","12","14","16","18","20","22","24","30","36","48","72" };
    
    public int showDialog(Font font) {
        setFont(font);
        return showDialog();
    }
    
    public int showDialog() {
        setVisible(true);
        
        return OPTION;
    }
    
    public JFontChooser(Frame parent) {
        super(parent, true);
        setTitle("Fuente");
        
        OPTION = JFontChooser.CANCEL_OPTION;
        
        // create all components
        
        JButton btnOK = new JButton("Aceptar");
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFontChooser.this.OPTION = JFontChooser.OK_OPTION;
                JFontChooser.this.setVisible(false);
            }
        });
        
        JButton btnCancel = new JButton("Cancelar");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFontChooser.this.OPTION = JFontChooser.CANCEL_OPTION;
                JFontChooser.this.setVisible(false);
            }
        });
        
        fontList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment().
                getAvailableFontFamilyNames()) {
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(150, 144); }
        };
        fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        
        sizeList = new JList(sizes) {
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(25, 144); }
        };
        sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        sizeField = new JTextField();
        
        cbBold = new JCheckBox("Negrita");
        
        cbItalic = new JCheckBox("Cursiva");
        
        
        txtSample = new JTextArea() {
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(385, 80); }
        };
        txtSample.setText("Visualizaci�n del tipo de fuente.");
        
        // set the default font
        
        setFont(null);
        
        
        // add the listeners
        
        ListSelectionListener listListener = new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                txtSample.setFont(getCurrentFont());
            }
        };
        
        fontList.addListSelectionListener(listListener);
        
        sizeList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                sizeField.setText((String)sizeList.getSelectedValue());
                txtSample.setFont(getCurrentFont());
            }
        });
        
        sizeField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                txtSample.setFont(getCurrentFont());
            }
            public void insertUpdate(DocumentEvent e) {
                txtSample.setFont(getCurrentFont());
            }
            public void removeUpdate(DocumentEvent e) {
                txtSample.setFont(getCurrentFont());
            }
        });
        
        ActionListener cbListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                txtSample.setFont(getCurrentFont());
            }
        };
        
        cbBold.addActionListener(cbListener);
        cbItalic.addActionListener(cbListener);
        
        // build the container
        
        getContentPane().setLayout(new java.awt.BorderLayout());
        
        JPanel sizePanel = new JPanel();
        sizePanel.setLayout(new java.awt.BorderLayout());
        sizePanel.add(new JScrollPane(sizeList), java.awt.BorderLayout.CENTER);
        sizePanel.add(sizeField, java.awt.BorderLayout.NORTH);
        
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new java.awt.BorderLayout());
        leftPanel.add(new JScrollPane(fontList), java.awt.BorderLayout.CENTER);
        leftPanel.add(sizePanel, java.awt.BorderLayout.EAST);
        
        
        
        getContentPane().add(leftPanel, java.awt.BorderLayout.CENTER);
        
        
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new java.awt.BorderLayout());
        
        JPanel rightPanelSub1 = new JPanel();
        rightPanelSub1.setLayout(new java.awt.FlowLayout());
        
        rightPanelSub1.add(cbBold);
        rightPanelSub1.add(cbItalic);
        
        rightPanel.add(rightPanelSub1, java.awt.BorderLayout.NORTH);
        
        JPanel rightPanelSub2 = new JPanel();
        rightPanelSub2.setLayout(new java.awt.GridLayout(2, 1));
        
        rightPanelSub2.add(btnOK);
        rightPanelSub2.add(btnCancel);
        
        rightPanel.add(rightPanelSub2, java.awt.BorderLayout.SOUTH);
        
        getContentPane().add(rightPanel, java.awt.BorderLayout.EAST);
        
        getContentPane().add(new JScrollPane(txtSample), java.awt.BorderLayout.SOUTH);
        
        setSize(200, 200);
        setResizable(false);
        
        pack();
    }
    
    public void setFont(Font font) {
        if (font == null) font = txtSample.getFont();
        
        fontList.setSelectedValue(font.getName(), true);
        fontList.ensureIndexIsVisible(fontList.getSelectedIndex());
        String strSize = "" + font.getSize();
        sizeField.setText(strSize);
        sizeList.setSelectedValue(strSize, true);
        sizeList.ensureIndexIsVisible(sizeList.getSelectedIndex());
        
        cbBold.setSelected(font.isBold());
        cbItalic.setSelected(font.isItalic());
    }
    
    public Font getFont() {
        if (OPTION == OK_OPTION) {
            return getCurrentFont();
        } else return null;
    }
    
    private Font getCurrentFont() {
        String fontFamily = (String)fontList.getSelectedValue();
//        String strFontSize = (String)sizeList.getSelectedValue();
        String strFontSize = sizeField.getText();
        int fontSize = 0;
        try {
            fontSize = Integer.parseInt(strFontSize);
        } catch (Exception ex) {
        }
        
        if (fontSize == 0)
            fontSize = 12;
        
        int fontType = Font.PLAIN;
        
        if (cbBold.isSelected()) fontType += Font.BOLD;
        if (cbItalic.isSelected()) fontType += Font.ITALIC;
        
        return new Font(fontFamily, fontType, fontSize);
    }
    
    // Prueba del dialogo
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFontChooser(new javax.swing.JFrame()).setVisible(true);
            }
        });
    }
}
